<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;
//使用类
use App\Http\Controllers\Controller;

class AdsController  extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        //分页数据
        $count = $request -> input('count',4);
        $search = $request -> input('search','');
        $request=$request->all();

        //查询数据库
        $data=\DB::table("ads")
            ->where("title",'like','%'.$search.'%')
            ->orderBy('sort','desc')
            ->paginate($count);
      
        // 加载页面
        return view("admin.sys.ads.index")->with('request',$request)->with("data",$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
       return view("admin.sys.ads.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   

        //接受表单数据

        $data=$request->except("_token","img");

       
         $filename=$request['img'];

      
         // 文件路径
         $temp_path = '/Uploads/ads'; 
         $img = ltrim($temp_path.'/'.$filename);
         $data['img']=$img;

         // 表单验证的规则

            $rules=[
                'title' => 'required|unique:ads', 
                'href' => 'required',
                'sort' => 'required',
                'img' => 'required',
            ];
            // 表单验证的提示信息

            $message=[

                "title.required"=>"请输入标题",
                "title.unique"=>"标题已存在",
                "href.required"=>"请输入链接",
                "sort.required"=>"请输入排序",
                "img.required"=>"请上传图片",
            ];

            // 使用laravel的表单验证
            $this->validate($request,$rules,$message);

            //插入数据库
            $res=\DB::table("ads")->insert($data);

             if($res){

                    return redirect('admin/sys/ads')->with('success','添加成功');
                }else{
                    return back()->with('error','添加失败');
                }
 
  }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        // 查询数据库

        $data=\DB::table("ads")->find($id);
        //dd($data);
        //分配数据
      return view('admin.sys.ads.edit')->with("data",$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
      
        $data=$request->except("_token","_method","img");
            //获取上传图片名称
        $filename=$request['img'];
        
        //对比
        $reg=\DB::table("ads")->where('id',$id)->first();
        
        if($data['title']==$reg['title']){
            unset($data['title']);
        }
      
         $rules=[
                'title' => 'required', 
                'href' => 'required',
            ];
            // 表单验证的提示信息

            $message=[

                "title.required"=>"请输入标题",
                "href.required"=>"请输入链接",
                "sort.required"=>"请输入排序",
            ];

            // 使用laravel的表单验证
            $this->validate($request,$rules,$message);

            if(!empty($filename)){

                     // 文件路径
                     $temp_path = '/Uploads/ads'; 
                     $img = ltrim($temp_path.'/'.$filename);
                     $data['img']=$img;

                    //删除原有文件
                     $filePath=$reg['img'];
                    
                    $re = \Storage::disk('public')->delete($filePath);
            }
       
       $res= \DB::table("ads")->where('id',$id)->update($data);

       if($res){

             return redirect('admin/sys/ads')->with('success','修改成功');

         }else{

             return back()->with('error','修改失败');
        }
       
       
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //


         //查找这个图片
        $reg=\DB::table("ads")->where('id',$id)->first();

         //删除原有文件
         $filePath=$reg['img'];

         
         $re = \Storage::disk('public')->delete($filePath);
        if (\DB::table("ads")->delete($id)) {
   
            return "1";
        }else{
            return 0;
        }
    }




     /**
     * 修改广告状态的方法
     *
     * @param  
     * @return \Illuminate\Http\Response
     */

    public function adsStatus(Request $request)
    {


        $arr=$request->except('_token');
       // dd($arr);
    // dd($arr);
        if (\DB::update("update ads set status= $arr[status] where id=$arr[id]")) {
            # code...
            echo 1;
        }else{
            echo 0;
         }
    }


    





}
